/*
 * fsys.cpp - playlist file manager and HAL DBus interface (add playlist mount device)
 *
 * Copyright (C) 2010-2011 Serge Begger
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "fsys.h"
#include "ui_fsys.h"

fsys::fsys(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::fsys)
{
    ui->setupUi(this);
    filter << "*.aac" << "*.mp3" << "*.flac" << "*.ape" << "*.ogg"
            << "*.ogm" << "*.ogv" << "*.mp4"  << "*.avi" << "*.ts"
            << "*.wv"  << "*.3gp" << "*.m2ts" << "*.vob" << "*.wmv"
            << "*.wav" << "*.mpg" << "*.mov"  << "*.m4a" << "*.m4v"
            << "*.mka" << "*.mkv" << "*.mpc";

    fsmodel.setFilter(QDir::AllDirs|QDir::Files|QDir::NoDotAndDotDot);
    fsmodel.setNameFilters(filter);

    ui->fsView->setModel(&fsmodel);

    on_backHomeButton_pressed();

}

fsys::~fsys()
{
    delete ui;
}

void fsys::on_backButton_pressed()
{
    this->hide();
    emit fsysHide();
}

void fsys::setDir(QString t_path, bool backward)
{
    if (QDir(t_path).exists()) {
        if (t_path == QDir::homePath())
            isHome = true;
        else
            isHome = false;

        ui->backFolderButton->setDisabled(isHome);

        if(!isHome && !backward)
            backPath.push(fsmodel.rootPath());
        fsmodel.setRootPath(t_path);
        QDir::setCurrent(t_path);
        ui->fsView->setRootIndex(fsmodel.index(t_path));
        //ui->pathLabel->setText(t_path);
    }
}

void fsys::on_fsView_clicked(const QModelIndex & index)
{
    if (fsmodel.isDir(index))
    {
        setDir(fsmodel.filePath(index),false);
    }
}

void fsys::on_backFolderButton_pressed()
{
    QString t_path = backPath.pop();
    setDir(t_path,true);

}

void fsys::on_backHomeButton_pressed()
{
    setDir(QDir::homePath(),false);
    isHome = true;
}

void fsys::addPath(QString path)
{
    addDirClicked(path);
}

void fsys::on_addButton_pressed()
{
    QString t_path = fsmodel.filePath(ui->fsView->currentIndex());
    if (fsmodel.isDir(ui->fsView->currentIndex())) {
        addDirClicked(t_path);
    } else
        addClicked(t_path);
}

void fsys::on_pushButUp_pressed()
{
    int currRow = ui->fsView->currentIndex().row() - 1;
    if (currRow < 0)
        currRow = 0;
    ui->fsView->setCurrentIndex(fsmodel.index(currRow,0,ui->fsView->rootIndex()));
}

void fsys::on_pushButDw_pressed()
{
    int currRow = ui->fsView->currentIndex().row() + 1;
    int rowCount = fsmodel.rowCount(ui->fsView->rootIndex());
    if (currRow > rowCount)
        currRow = rowCount;
    ui->fsView->setCurrentIndex(fsmodel.index(currRow,0,ui->fsView->rootIndex()));
}

void fsys::addClicked(QString filePath)
{
    emit addSource(filePath);
}

void fsys::addDirClicked(QString dirPath)
{
    QDir d(dirPath);

    QStringList files = d.entryList(filter, QDir::Files, QDir::Name);
    foreach (QString str, files) {
        emit addSource(dirPath+"/"+str);
    }

    QStringList dirs = d.entryList(QDir::Dirs, QDir::Name);
    foreach (QString str, dirs) {
        if (str != "." && str != "..")
            addDirClicked(dirPath+"/"+str);
    }
}


